<template>
  <div>
    <div style="background-color: #1B2330;">
      <a-button
        style="margin: 12px 0 28px 12px;"
        type="primary"
        @click="() => $emit('hide-detail')"
      >
        返回
      </a-button>
    </div>

    <!-- section1 -->
    <div class="overview-bg">
      <div class="overview-col-item">
        <div>
          <div class="item-title">
            <span>L1层：基础设施</span>
          </div>
          <div
            style="width: 95%;"
            class="item-content"
          >
            <div class="icon-wrapper">
              <div class="icon-content">
                <img src="@assets/overview_air.png" />
                <div>
                  <p>空调</p>
                  <span>{{resourceData.airConditionNum || 0}}台</span>
                </div>
              </div>
              <div class="icon-content">
                <img src="@assets/overview_electricity.png" />
                <div>
                  <p>配电箱</p>
                  <span>{{resourceData.distributionBoxNum || 0}}台</span>
                </div>
              </div>
              <div class="icon-content">
                <img src="@assets/overview_ups.png" />
                <div>
                  <p>UPS</p>
                  <span>{{resourceData.upsNum || 0}}台</span>
                </div>
              </div>
            </div>

            <div class="icon-wrapper">
              <div
                class="card-title"
                style="margin-left: 20px"
              >
                <span>环境</span>
              </div>
              <Thermometer
                :temperature="temperature"
                :humidity="humidity"
              />
            </div>

            <div class="icon-wrapper">
              <div class="card-title">
                <span>能效</span>
              </div>
              <Gauge :pue="pue" />
            </div>
          </div>
        </div>

        <!-- section2 -->
        <div>
          <div class="item-title">
            <span>L2层：IT设备</span>
          </div>
          <div style="display: flex; justify-content: flex-start;">
            <div class="item-content card-box">
              <div class="card-wrapper">
                <div class="card-title">
                  <span>服务器</span>
                </div>
                <div class="card-content">
                  <a-icon
                    type="desktop"
                    class="card-icon"
                    style="color: rgb(255,143,24)"
                  />
                  <div>
                    <p>物理机</p>
                    <span>{{resourceData.serverNum || 0}}台</span>
                  </div>
                </div>
              </div>
            </div>

            <div class="item-content card-box">
              <div class="card-wrapper">
                <div class="card-title">
                  <span>交换机</span>
                </div>
                <div class="card-content">
                  <img src="@assets/overview_switcher.png" />
                  <div>
                    <p>交换机</p>
                    <span>{{resourceData.switcherNum || 0}}台</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- section3 -->
      <div class="overview-col-item">
        <div class="overview-right-item">
          <div
            style="float: right;"
            class="item-title"
          >
            <span>L3层：云平台资源</span>
          </div>
          <div>
            <div class="item-content card-box">
              <div class="card-wrapper">
                <div class="card-title">
                  <span>虚拟机概览</span>
                </div>
                <div class="card-content">
                  <a-icon
                    type="cloud-upload"
                    class="card-icon"
                  />
                  <div>
                    <p>虚拟机</p>
                    <span>{{vmNum}}台</span>
                  </div>
                </div>
              </div>
            </div>

            <div class="item-content card-box">
              <div class="card-wrapper">
                <div class="card-title">
                  <span>容器概览</span>
                </div>
                <div class="card-content">
                  <a-icon
                    type="code-sandbox"
                    class="card-icon"
                  />
                  <div>
                    <p>容器集群</p>
                    <span>{{dockerNum}}个</span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        <!-- section4 -->
        <div class="overview-right-item">
          <div class="item-title">
            <span>L4层：应用资源</span>
          </div>
          <div>
            <div
              class="card-wrapper"
              style="margin: 3px 0; padding-left: 10px; height: 95px; width: 230px; background-color: rgba(30, 159, 255, 0.18);"
            >
              <div class="card-content">
                <a-icon
                  type="dropbox"
                  class="col-app-icon"
                />
                <div>
                  <p>应用数</p>
                  <span>{{appResourceData.total}}个</span>
                </div>
              </div>
            </div>
          </div>
          <div>
            <div
              class="item-content"
              style="flex-direction: column;"
            >
              <div
                class="resource-info-wrapper"
                v-for="(item, i) in appResourceList"
                :key="i"
              >
                <div
                  v-for="d in item"
                  :key="d.key"
                  class="resource-info-card"
                >
                  <span>{{d.label}}</span>
                  <span>{{appResourceData[d.key]}}个</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Gauge from './components/Gauge'
  import Thermometer from './components/Thermometer'
  import { gPostAction } from '@/api/manage'

  export default {
    name: 'OverView',
    props: {
      dataCenterId: {
        type: String,
        default: ''
      }
    },
    components: { Gauge, Thermometer },
    data() {
      return {
        appResourceList: {
          section1: [{
            key: 'cpuNum',
            label: '计算密集型'
          }, {
            key: 'ioNum',
            label: '磁盘IO密集型'
          }, {
            key: 'aiNum',
            label: '人工智能型'
          }],
          section2: [{
            key: 'offlineNum',
            label: '离线型'
          }, {
            key: 'realtimeNum',
            label: '实时型'
          }, {
            key: 'sequentialNum',
            label: '时序型'
          }],
        },
        resourceData: {},
        appResourceData: {},
        vmNum: 0,
        dockerNum: 0,
        temperature: 0,
        humidity: 0,
        pue: 0
      }
    },
    mounted() {
      this.loadCountItDevice()
      this.loadCountAppProfile()
      this.loadCloudResource(0)
      this.loadCloudResource(1)
      this.loadLatestSample(0)
      this.loadLatestSample(1)
      this.loadLatestSample(2)
    },
    methods: {
      loadCountItDevice() {
        const postData = {
          datacenterId: this.dataCenterId,
        }

        gPostAction('/asset/countItDevice', postData).then(res => {
          if (res.success) {
            this.resourceData = res.data
          }
        })
      },
      loadCountAppProfile() {
        const postData = {
          datacenterId: this.dataCenterId,
          title: "精确管控与供电制冷联动-基础资源细粒度感知-模型总览图",
          content: "查看模型总览图"
        }

        gPostAction('/profile/countAppProfile', postData).then(res => {
          if (res.success) {
            this.appResourceData = res.data
          }
        })
      },
      loadCloudResource(type) {
        const postData = {
          pageSize: 9999,
          datacenterId: this.dataCenterId,
          type
        }
        gPostAction('/cloud_resource/page', postData).then(res => {
          if (res.success) {
            if (type === 0) {
              this.vmNum = res.data.totalCount
            } else if (type === 1) {
              this.dockerNum = res.data.totalCount
            }
          }
        })
      },
      loadLatestSample(key) {
        const meter = ['environment.air_out_temperature', 'environment.air_out_humidity', 'datacenter.pue']
        const postData = {
          meter: meter[key],
          resourceId: this.dataCenterId
        }

        gPostAction('/statistics/latestSample', postData).then(res => {
          if (res.success) {
            const num = parseFloat(res.data || 0)
            if (key === 0) {
              this.temperature = num
            } else if (key === 1) {
              this.humidity = num
            } else if (key === 2) {
              this.pue = num
            }
          }
        })
      },
    },
  }
</script>

<style
  lang="less"
  scoped
>
  .overview-bg {
    display: flex;
    justify-content: space-between;
    width: 100%;
    height: 90vh;
    min-height: 850px;
    text-align: center;
    background: #1B2330 url('../../../../../assets/overview_bg.png') no-repeat 54% 53%;
    background-size: 48% auto;
    padding-bottom: 30px;

    .overview-col-item {
      display: flex;
      flex-direction: column;
      justify-content: space-between;

      .overview-right-item {
        display: flex;
        flex-direction: column;
        align-items: flex-end;
      }

      .item-title {
        padding: 8px 18px;
        width: 150px;
        text-align: center;
        color: #18C6FF;
        background-color: rgba(26, 45, 93, 100);
        font-size: 14px;
        border-radius: 5px 5px 0 0;
      }

      .item-content {
        display: flex;
        flex-direction: row;
        padding: 16px 18px 12px 24px;
        background-color: rgba(30, 159, 255, .18);

        .card-title {
          display: flex;
          align-items: center;
          padding: 4px 0 4px 10px;
          height: 21px;
          border-left: 5px solid #0784D3;
          color: #0784D3;

          span {
            font-size: 16px;
          }
        }

        /* &.card-box {
          display: inline-block;
        } */

        /* section1 */
        .icon-wrapper {
          &:first-child {
            margin-top: 29px;
          }

          &:not(:first-child) {
            margin-left: 16px;
          }

          .icon-content {
            margin-left: 22px;
            display: flex;
            align-items: center;
            color: #18C6FF;

            &:not(:last-child) {
              margin-bottom: 12px;
            }

            img {
              margin-right: 12px;
              width: 48%;
            }

            div {
              text-align: left;
              min-width: 45px;

              p {
                margin-bottom: 4px;
              }

              span {
                font-weight: bolder;
                font-size: 20px;
              }
            }
          }
        }

        /* section4 */
        .resource-info-wrapper {
          display: flex;
          justify-content: space-around;

          .resource-info-card {
            display: flex;
            align-items: center;
            padding: 12px 0 8px 20px;
            /* width: 196px; */
            background-color: #1B4569;
            border-radius: 6px;
            color: #18C6FF;
            font-size: 18px;

            &:not(:last-child) {
              margin-right: 20px;
            }

            /* & span:first-child {
              width: 108px;
            } */

            & span:last-child {
              margin-left: 6px;
              font-weight: bolder;
              font-size: 22px;
            }
          }

          &:not(:last-child) {
            margin-bottom: 20px;
          }

          &:last-child {
            margin-bottom: 8px;
          }
        }
      }

      /* section2 3 */
      .card-wrapper {
        padding: 8px 0 8px 7%;
        margin: 4px 0 10px 0;
        width: 200px;
        height: 115px;
        background-color: #1B4569;
        border-radius: 6px;

        .card-content {
          padding: 12px 0 0 18%;
          display: flex;
          align-items: center;
          color: #18C6FF;

          .col-app-icon {
            color: rgb(255, 143, 24);
            font-size: 55px;
            margin-right: 25px;
          }

          .card-icon {
            font-size: 55px;
            color: rgb(151, 229, 255);
            margin-right: 15px;
          }

          img {
            margin-right: 12px;
          }

          p {
            margin-bottom: 2px;
          }

          div {
            min-width: 60px;
          }

          span {
            font-weight: bolder;
            font-size: 22px;
          }
        }
      }
    }
  }
</style>